package com.sunny.myproject.core.base.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.springframework.ui.Model;

import com.sunny.myproject.core.base.util.Pager;

public abstract class BaseAction<T> implements BaseMethod<T> {

    protected Pager pager = new Pager();
    protected Map<String, Object> map = new HashMap<String, Object>();
    protected List<Criterion> criterions = new ArrayList<Criterion>();
    protected List<Projection> projections = new ArrayList<Projection>();
    protected List<Order> orders = new ArrayList<Order>();

    /**
     * 分页
     *
     * @param request
     * @return
     */
    protected Pager pageStart(Model model, HttpServletRequest request) {

        criterions = new ArrayList<Criterion>();

        projections = new ArrayList<Projection>();

        orders = new ArrayList<Order>();

        map = new HashMap<String, Object>();

        Pager pager = new Pager();
        Integer start = 1;
        Integer limit = 10;

        if (request.getParameter("pageNum") != null) {
            start = Integer.parseInt(request.getParameter("pageNum"));
        }
        if (request.getParameter("limit") != null) {
            limit = Integer.parseInt(request.getParameter("limit"));
        }

        pager.setPageNum(start);
        pager.setNumPerPage(limit);

        model.addAttribute("pageNum", pager.getPageNum());
        return pager;
    }

    public Pager pageEnd(Model model, Integer totalCount, Pager pager) {
        
    	try {
    		 pager.setTotalCount(totalCount);

    	        Integer pageInteger = totalCount % pager.getNumPerPage();
    	        if (pageInteger > 0) {
    	            pager.setTotalPage(totalCount/pager.getNumPerPage() + 1);
    	        } else {
    	            pager.setTotalPage(totalCount/pager.getNumPerPage());
    	        }

    	        pager.setMaxResults(pager.getNumPerPage());
    	        
    	        pager.setFirstResults((pager.getPageNum()-1)*pager.getNumPerPage());
    	        model.addAttribute("totalPage", pager.getTotalPage());
    	        model.addAttribute("totalCount", pager.getTotalCount());
		} catch (Exception e) {
			e.printStackTrace();
		}
        return pager;
    }

    public void returnValue(Model model, Object obj) {
        model.addAttribute("t", obj);
    }

    public void returnViewValue(Model model,Object obj){
         model.addAttribute("t", obj);
    	 model.addAttribute("status", "view");
    }
    
    public Long param1(HttpServletRequest request) {
        Long ids = Long.parseLong(request.getParameter("ids"));
        return ids;
    }

    public Long[] param2(HttpServletRequest request) {
        String idsS = request.getParameter("ids");
        idsS = idsS.replace("on,", "");
        String[] idsStrings = idsS.split(",");
        Long[] ids = new Long[idsStrings.length];
        for (int i = 0; i < idsStrings.length; i++) {
            ids[i] = Long.parseLong(idsStrings[i]);
        }
        return ids;
    }
}
